package com.template.controller;

import com.template.common.result.Result;
import com.template.common.result.ResultCode;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 测试控制器
 * 
 * @author template
 * @since 2024-01-01
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @GetMapping("/hello")
    public Result<String> hello() {
        return Result.success("Hello, SpringBoot Template!");
    }

    @GetMapping("/info")
    public Result<Map<String, Object>> getInfo() {
        Map<String, Object> info = new HashMap<>();
        info.put("name", "SpringBoot通用模板");
        info.put("version", "1.0.0");
        info.put("description", "基于SpringBoot2.6的通用后端开发模板");
        info.put("author", "template");
        info.put("time", LocalDateTime.now());
        return Result.success(info);
    }

    @GetMapping("/error")
    public Result<String> testError() {
        throw new RuntimeException("这是一个测试异常");
    }

    @GetMapping("/business-error")
    public Result<String> testBusinessError() {
        throw new com.template.common.exception.BusinessException("这是一个业务异常");
    }

    @PostMapping("/validation")
    public Result<String> testValidation(@RequestParam(required = false) String name,
                                        @RequestParam(required = false) Integer age) {
        if (name == null || name.trim().isEmpty()) {
            throw new com.template.common.exception.BusinessException(ResultCode.PARAM_ERROR, "姓名不能为空");
        }
        if (age == null || age < 0) {
            throw new com.template.common.exception.BusinessException(ResultCode.PARAM_ERROR, "年龄必须大于等于0");
        }
        return Result.success("参数校验通过");
    }
}
